package com.itlong.cloud.commons.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.AppRobotOperateLogPO;
import com.itlong.cloud.commons.service.IRobotLoginLogService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.node.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <desc>
 *      机器人登录日志服务层接口实现类
 * </desc>
 *
 * @createDate 2018/10/15
 */
@Service
public class RobotLoginLogServiceImpl implements IRobotLoginLogService {

    @Autowired
    private MessageSender messageSender;

    /**
     * <desc>
     *      保存机器人登录日志信息
     * </desc>
     *
     * @param appRobotOperateLogPO 登陆日志实体对象
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/10/15
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(AppRobotOperateLogPO appRobotOperateLogPO) throws Exception {
        if (appRobotOperateLogPO != null) {
            // 将要保存的登录日志发送到消息队列
            JSONObject dataJs = new JSONObject();
            dataJs.put("data",appRobotOperateLogPO);
            messageSender.send(RabbitMQConstant.RABBITMQ_ROBOT_LOGIN_LOG_SAVE_QUEUE,
                    JsonUtil.toJSON(dataJs), RabbitMQConstant.RABBITMQ_USER_EXCHANGE,
                    RabbitMQConstant.RABBITMQ_ROBOT_LOGIN_LOG_SAVE_ROUTING_KEY);
            return 1;
        }
        return null;
    }
}
